package com.xckj.ibox.presenter;

import android.content.Intent;

import androidx.annotation.NonNull;

import com.xckj.ibox.App;
import com.xckj.ibox.bean.BannerBean;
import com.xckj.ibox.bean.BaseBean;
import com.xckj.ibox.bean.UserInfoBean;
import com.xckj.ibox.framework.net.RetrofitManager;
import com.xckj.ibox.global.Constant;
import com.xckj.ibox.iview.IMyView;
import com.xckj.ibox.ui.activity.HomeActivity;
import com.xckj.ibox.ui.activity.LoginActivity;

import java.util.List;
import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author: ChenYao
 * @Describe: 我的
 * @Date: 2023/6/16 18:23
 */
public class MyPresenter {
    private final IMyView myView;

    public MyPresenter(IMyView myView) {
        this.myView = myView;
    }

    /**
     * 获取用户信息
     *
     * @param tokens token
     */
    public void getUserInfo(String tokens) {
        myView.showLoading();
        Call<BaseBean<UserInfoBean>> call = RetrofitManager.getInstance().getService().getUserInfo(tokens);
        call.enqueue(new Callback<BaseBean<UserInfoBean>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<UserInfoBean>> call, @NonNull Response<BaseBean<UserInfoBean>> response) {
                if (response.isSuccessful()) {
                    BaseBean<UserInfoBean> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        myView.getUserInfoSuccess(baseBean.data);
                    } else if (Constant.TOKEN_LOSE_CODE == baseBean.code) {
                        myView.tokenLose();
                    } else {
                        myView.failed(baseBean.msg);
                    }
                } else {
                    myView.failed("请求失败");
                }
                myView.hideDialog();
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<UserInfoBean>> call, @NonNull Throwable t) {
                t.printStackTrace();
                myView.failed("请求失败");
                myView.hideDialog();
            }
        });

    }
}
